<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <my-search @click="goToSearchList"></my-search>

    <view class="scroll-view-container">
      <!-- 左侧的滚动区域 -->
      <scroll-view
        class="left-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <block v-for="(item1, index1) in categoryList" :key="index1">
          <view
            class="left-scroll-view-item"
            :class="{ active: categoryIndex === index1 }"
            @click="selectCategory(index1)"
            >{{ item1.cat_name }}</view
          >
        </block>
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
        :scroll-top="scrollTop"
      >
        <view
          class="cate-lv2"
          v-for="(item2, index2) in categoryList[categoryIndex].children"
          :key="index2"
        >
          <!-- 二级标题 -->
          <view class="cate-lv2-title">/ {{ item2.cat_name }} /</view>
          <!-- 三级 -->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item3, index3) in item2.children"
              :key="index3"
              @click="goToProductList(item3)"
            >
              <image :src="item3.cat_icon"></image>
              <text>{{ item3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import MySearch from "@/components/my-search.vue";
import badge from "@/mixins/badge";
export default {
  mixins: [badge],
  components: {
    MySearch,
  },
  data() {
    return {
      categoryList: [],
      wh: 0, // 窗口的可用高度
      categoryIndex: 0, // 记录左侧选中的下标
      scrollTop: 0, // 滚动到顶部
    };
  },
  created() {
    // 1. 获取系统信息
    const systemInfo = uni.getSystemInfoSync();
    // 可用的高度 = 屏幕的高度 - 导航栏的高度 - tabbar的高度 - 搜索框的高度
    this.wh = systemInfo.windowHeight - 50;

    // 2. 获取分类数据
    this.getCategoryList();
  },
  methods: {
    // 1. 获取分类列表
    async getCategoryList() {
      const { meta, message } = await this.$http.get("/categories");
      // 1.1 提示用户
      if (meta.status !== 200) return this.$msg("获取分类数据失败");
      // 1.2 更新数据
      this.categoryList = message;
      // console.log(this.categoryList);
    },
    // 2. 切换一级分类
    selectCategory(index) {
      // console.log(index);
      this.categoryIndex = index;
      // 让偏移量有一点改变
      this.scrollTop = this.scrollTop === 0 ? 1 : 0;
    },
    // 3. 跳转去商品列表页
    goToProductList(item) {
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?cid=${item.cat_id}`,
      });
    },
    // 4. 去搜素页面
    goToSearchList() {
      // console.log("去搜索页面");
      uni.navigateTo({
        url: "/pages_sub1/search/search",
      });
    },
  },
};
</script>

<style scoped lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 120rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 24rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: " ";
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
}

.cate-lv2-title {
  font-size: 24rpx;
  font-weight: bold;
  text-align: center;
  padding: 30rpx 0;
}

.cate-lv3-list {
  display: flex;
  flex-wrap: wrap;

  .cate-lv3-item {
    width: 33.33%;
    margin-bottom: 20rpx;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 120rpx;
      height: 120rpx;
    }

    text {
      font-size: 24rpx;
    }
  }
}
</style>